#include <msp430.h> 


/**
 * main.c
 */

volatile int flag;
volatile int check;


int main(void)
{
	WDTCTL = WDTPW | WDTHOLD;	// stop watchdog timer
	P1DIR |= BIT5;
	P4DIR |= BIT5;
	P4DIR &= ~BIT0;
	P4REN |= BIT0;
	P4OUT |= BIT0;
	P4IES |= BIT0;
	P4IFG &= ~BIT0;
	P4IE |= BIT0;
	flag = 0;

	TA0CTL |= MC_1 + TASSEL_2 + TACLR;
	
	TA0CCTL0 = CCIE;
	TA0CCR0 = 50000;
	__bis_SR_register(LPM0_bits+GIE);
	return 0;
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A(void)
{
    if(flag)
    {
        P1OUT ^= BIT5;
        P4OUT ^= BIT5;
    }
}

#pragma vector = PORT4_VECTOR
__interrupt void Port_4(void)
{
//    P4OUT ^= BIT5;
//    flag = !flag;
//    P4IFG &= ~BIT0;
    TA1CTL |= MC_1 + TASSEL_2 + TACLR;
    check = 1;
    TA1CCTL0 = CCIE;
    TA1CCR0 = 1000;
    P4IFG &= ~BIT0;
}

#pragma vector = TIMER1_A0_VECTOR
__interrupt void Timer1_A(void)
{
    check = check << 1;
    if(P4IN & BIT0)
    {
        check += 1;
    }
    if(check & (0x7) == 0x7){
        flag = !flag;
        TA1CCTL0 ^= CCIE;
    }
}
